﻿@page "/posts/year/{ForYear:int}"
@inject MarkdownBlog Blog
@inject AppConfig AppConfig

@if (allYears.Count > 0)
{
    <PageTitle>@ForYear posts</PageTitle>

    <div class="relative bg-gray-50 dark:bg-gray-900 px-6 pt-16 pb-20 lg:px-8 lg:pt-24 lg:pb-28">
        <div class="absolute inset-0">
            <div class="h-1/3 bg-white dark:bg-black sm:h-2/3"></div>
        </div>
        <div class="relative mx-auto max-w-7xl">
            <BlogTitle Heading=@($"All posts published in <b>{ForYear}</b>") />
        </div>
        <div class="mt-4 relative mb-8 mx-auto max-w-7xl">
            <div class="flex flex-wrap justify-center">
                @foreach (var year in allYears)
                {
                    if (ForYear == year)
                    {
                        <b class="ml-3 text-sm font-semibold">@year</b>
                    }
                    else
                    {
                        <a class="ml-3 text-sm text-indigo-700 font-semibold hover:underline" href="@Blog.GetYearLink(year)">@year</a>
                    }
                }
            </div>
        </div>
        <div class="mt-12 relative mx-auto max-w-7xl">
            <BlogPosts Posts=@Blog.GetPosts(year: ForYear) />

            <div class="mt-8 text-center">
                <a class="text-sm font-semibold hover:underline" href="@Blog.GetPostsLink()">view all posts</a>
            </div>
        </div>

    </div>
}
else
{
    <div class="mt-3 mb-20 mx-auto max-w-fit">
    @if (error != null)
    {
        <ErrorSummary Status=@error />
    }
    else
    {
        <Loading />
    }
    </div>
}

@code {
    [Parameter] public required int ForYear { get; set; }

    List<int> allYears = new();
    ResponseStatus? error;

    void load()
    {
        allYears = Blog.VisiblePosts.Select(x => x.Date.Value.Year).Distinct().OrderByDescending(x => x).ToList();
    }

    protected override void OnInitialized() => load();

    protected override void OnParametersSet() => load();
}
